package com.jml.nio;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;

public class 编码格式 {


	@Test
	public void fun5() throws CharacterCodingException {
		//指定编码方式
		Charset charset = Charset.forName("UTF-8");
		//获取UTF-8编码器
		CharsetEncoder charsetEncoder = charset.newEncoder();
		//获取UTF-8解码器
		CharsetDecoder charsetDecoder = charset.newDecoder();

		CharBuffer charBuffer = CharBuffer.allocate(1024);
		charBuffer.put("编码和解码!");
		charBuffer.flip();
		//编码
		ByteBuffer byteBuffer = charsetEncoder.encode(charBuffer);

		for (int i = 0; i < 12; i++) {
			System.out.println(byteBuffer.get());
		}
		//解码
		byteBuffer.flip();
		CharBuffer charBuffer1 = charsetDecoder.decode(byteBuffer);
		System.out.println(charBuffer1.toString());
	}

	@Test
	public void fun4(){
		//查看支持哪些字符编码
		Map<String, Charset> map = Charset.availableCharsets();
		Set<Entry<String, Charset>> set = map.entrySet();
		for (Map.Entry<String, Charset> entry : set) {
			System.out.println(entry.getKey()+"="+entry.getValue());
		}
	}

}
